from pydantic import BaseModel, validator
from typing import Optional, List
from datetime import datetime, date
from enum import Enum

class UserRole(str, Enum):
    """用户角色枚举"""
    ADMIN = "admin"
    USER = "user"

class WeatherType(str, Enum):
    """天气类型枚举"""
    SUNNY = "晴天"
    CLOUDY = "多云"
    OVERCAST = "阴天"
    LIGHT_RAIN = "小雨"
    RAIN = "雨天"
    LIGHT_SNOW = "小雪"
    SNOW = "雪天"

class BorrowStatus(str, Enum):
    """借阅状态枚举"""
    BORROWED = "已借出"
    RETURNED = "已归还"
    OVERDUE = "逾期"
    LOST = "丢失"

# 用户相关模型
class UserBase(BaseModel):
    """用户基础模型"""
    username: str
    name: str
    role: UserRole = UserRole.USER

class UserCreate(UserBase):
    """创建用户模型"""
    password: str

class UserLogin(BaseModel):
    """用户登录模型"""
    username: str
    password: str

class UserResponse(UserBase):
    """用户响应模型"""
    id: int
    
    class Config:
        from_attributes = True

class LoginResponse(BaseModel):
    """登录响应模型"""
    success: bool
    message: str
    user: Optional[UserResponse] = None
    token: Optional[str] = None

# 天气数据相关模型
class WeatherBase(BaseModel):
    """天气数据基础模型"""
    date: date
    location: str
    temperature: float
    weather: WeatherType
    humidity: int
    wind_speed: float
    
    @validator('humidity')
    def validate_humidity(cls, v):
        if not 0 <= v <= 100:
            raise ValueError('湿度必须在0-100之间')
        return v
    
    @validator('temperature')
    def validate_temperature(cls, v):
        if not -50 <= v <= 60:
            raise ValueError('温度必须在-50到60度之间')
        return v
    
    @validator('wind_speed')
    def validate_wind_speed(cls, v):
        if v < 0:
            raise ValueError('风速不能为负数')
        return v

class WeatherCreate(WeatherBase):
    """创建天气数据模型"""
    pass

class WeatherUpdate(BaseModel):
    """更新天气数据模型"""
    date: Optional[date] = None
    location: Optional[str] = None
    temperature: Optional[float] = None
    weather: Optional[WeatherType] = None
    humidity: Optional[int] = None
    wind_speed: Optional[float] = None

class WeatherResponse(WeatherBase):
    """天气数据响应模型"""
    id: int
    created_at: datetime
    
    class Config:
        from_attributes = True

class WeatherQuery(BaseModel):
    """天气数据查询模型"""
    location: Optional[str] = None
    weather: Optional[WeatherType] = None
    start_date: Optional[date] = None
    end_date: Optional[date] = None
    page: int = 1
    page_size: int = 20

class WeatherListResponse(BaseModel):
    """天气数据列表响应模型"""
    data: List[WeatherResponse]
    total: int
    page: int
    page_size: int
    total_pages: int

class WeatherStats(BaseModel):
    """天气数据统计模型"""
    total_records: int
    total_cities: int
    average_temperature: float
    today_records: int
    recent_data: List[WeatherResponse]

# 图书借阅数据相关模型
class BookBorrowBase(BaseModel):
    """图书借阅数据基础模型"""
    borrow_date: date
    borrower: str
    borrowed_book: str
    book_unit_price: float
    borrow_status: BorrowStatus
    
    @validator('book_unit_price')
    def validate_price(cls, v):
        if v <= 0:
            raise ValueError('图书单价必须大于0')
        return v
    
    @validator('borrower')
    def validate_borrower(cls, v):
        if not v or len(v.strip()) == 0:
            raise ValueError('借阅者姓名不能为空')
        return v.strip()
    
    @validator('borrowed_book')
    def validate_book(cls, v):
        if not v or len(v.strip()) == 0:
            raise ValueError('图书名称不能为空')
        return v.strip()

class BookBorrowCreate(BookBorrowBase):
    """创建图书借阅数据模型"""
    pass

class BookBorrowUpdate(BaseModel):
    """更新图书借阅数据模型"""
    borrow_date: Optional[date] = None
    borrower: Optional[str] = None
    borrowed_book: Optional[str] = None
    book_unit_price: Optional[float] = None
    borrow_status: Optional[BorrowStatus] = None

class BookBorrowResponse(BookBorrowBase):
    """图书借阅数据响应模型"""
    id: int
    created_at: datetime
    
    class Config:
        from_attributes = True

class BookBorrowQuery(BaseModel):
    """图书借阅数据查询模型"""
    borrower: Optional[str] = None
    borrowed_book: Optional[str] = None
    borrow_status: Optional[BorrowStatus] = None
    start_date: Optional[date] = None
    end_date: Optional[date] = None
    page: int = 1
    page_size: int = 20

class BookBorrowListResponse(BaseModel):
    """图书借阅数据列表响应模型"""
    data: List[BookBorrowResponse]
    total: int
    page: int
    page_size: int
    total_pages: int

class BookBorrowStats(BaseModel):
    """图书借阅数据统计模型"""
    total_records: int
    total_borrowers: int
    total_books: int
    average_price: float
    today_records: int
    recent_data: List[BookBorrowResponse]

class APIResponse(BaseModel):
    """通用API响应模型"""
    success: bool
    message: str
    data: Optional[dict] = None 